package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.RegistrationStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.RegistrationStatRequest;
import com.tuanzi.loan.vo.response.statistics.RegistrationStatResponse;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.Objects;

import static com.tuanzi.loan.generated.jooq.stat.tables.StatCommUserInfo.STAT_COMM_USER_INFO;
@Repository
public class RegistrationStatRepositoryImpl extends JOOQBaseRepositoryPlus implements RegistrationStatRepository{
    @Override
    protected TableImpl getTable() {
        return STAT_COMM_USER_INFO;
    }
    @Override
    public Page<RegistrationStatResponse> list(RegistrationStatRequest searchRequest, Pageable pageable) {
        fields(
                STAT_COMM_USER_INFO.TIME,
                STAT_COMM_USER_INFO.PRD_ID,
                DSL.sum(STAT_COMM_USER_INFO.REGISTNUM).as(STAT_COMM_USER_INFO.REGISTNUM),
                DSL.sum(STAT_COMM_USER_INFO.HACTIVEREGIST).as(STAT_COMM_USER_INFO.HACTIVEREGIST),
                DSL.sum(STAT_COMM_USER_INFO.APPACTIVEREGIST).as(STAT_COMM_USER_INFO.APPACTIVEREGIST),
                DSL.sum(STAT_COMM_USER_INFO.ACTIVEREGIST).as(STAT_COMM_USER_INFO.ACTIVEREGIST)
        );

        if (Objects.nonNull(searchRequest.getStartDate())){
            ge(STAT_COMM_USER_INFO.TIME,searchRequest.getStartDate());
        }

        if (Objects.nonNull(searchRequest.getEndDate())){
            le(STAT_COMM_USER_INFO.TIME,searchRequest.getEndDate());
        }

        if (Objects.isNull(searchRequest.getStartDate())&&Objects.isNull(searchRequest.getEndDate())){
            between(STAT_COMM_USER_INFO.TIME,searchRequest.getStartDate(),
                    searchRequest.getEndDate());
        }
        if (!(searchRequest.getPrd_id() != null && searchRequest.getPrd_id().contains("-2000"))){
            in(STAT_COMM_USER_INFO.PRD_ID,searchRequest.getPrd_id());
            groupBy(STAT_COMM_USER_INFO.PRD_ID);
        }
        groupBy(STAT_COMM_USER_INFO.TIME);
        orderBy(STAT_COMM_USER_INFO.TIME, Sort.Direction.DESC);
        return search(pageable,record -> {
            RegistrationStatResponse registrationStatResponse = new RegistrationStatResponse();
            registrationStatResponse.setActiveregist(record.get(STAT_COMM_USER_INFO.ACTIVEREGIST,Integer.class));
            registrationStatResponse.setAppactiveregist(record.get(STAT_COMM_USER_INFO.APPACTIVEREGIST,Integer.class));
            registrationStatResponse.setHactiveregist(record.get(STAT_COMM_USER_INFO.HACTIVEREGIST,Integer.class));
            registrationStatResponse.setRegistnum(record.get(STAT_COMM_USER_INFO.REGISTNUM,Integer.class));
            registrationStatResponse.setTime(record.get(STAT_COMM_USER_INFO.TIME));
            if (searchRequest.getPrd_id() != null && searchRequest.getPrd_id().contains("-2000")){
                registrationStatResponse.setPrd_id("全部");
            }else{
                registrationStatResponse.setPrd_id(record.get(STAT_COMM_USER_INFO.PRD_ID));
            }
            return  registrationStatResponse;
        });
    }
}
